package xyz.sicau.blog.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@Slf4j
@RequestMapping("/redis")
public class RedisController {

    public static final String ORDER_KEY = "ord:";

//    @Resource
//    private RedisTemplate redisTemplate;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/add")
    public void addOrder() {
        int keyId = ThreadLocalRandom.current().nextInt(1000) + 1;
        String serialNo = UUID.randomUUID().toString();

        String key = ORDER_KEY + keyId;
        String value = "订单" + serialNo;

        stringRedisTemplate.opsForValue().set(key, value);

        log.info("key:{}", key);
        log.info("value:{}", value);
    }

    @RequestMapping("/get")
    public String getOrder(int id) {
        System.out.println(stringRedisTemplate.opsForValue().get(ORDER_KEY + id));
        return (String) stringRedisTemplate.opsForValue().get(ORDER_KEY + id);
    }
}
